package com.kingyun.gpsinspection.purificationservice.services.iam.innerservice.impl;

import com.cqhilink.api.common.utils.date.DateStyle;
import com.cqhilink.api.common.utils.date.DateUtil;
import com.kingyun.gpsinspection.purificationservice.services.hbaseservice.HbaseUtils;
import com.kingyun.gpsinspection.purificationservice.services.iam.innerservice.TemptasksService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.Date;
import java.util.Random;

/**
 * Created by yangcs on 2016/8/7.
 */
@Service
public class TemptasksServiceImpl implements TemptasksService {
    private static Logger logger = LoggerFactory.getLogger(TemptasksServiceImpl.class);
    @Override
    public int delfile(String hbasepath) {
        return 1 ;
        //return temptasksMapper.delfile(hbasepath);
    }

    @Override
    public String[] uploadimg(HttpServletRequest request, InputStream input) throws Exception {
        String hbasepath = DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM_SS_SSS_EN) + "_" + (new Random().nextInt(9000) + 1000) + "_file";
        ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext());
        logger.info("HBase Application Running");
        HbaseUtils userUtils = context.getBean(HbaseUtils.class);
        userUtils.initialize("file");
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        byte[] buffer = new byte[4096];
        int n = 0;
        while (-1 != (n = request.getInputStream().read(buffer))) {
            output.write(buffer, 0, n);
        }
        userUtils.addOrg(hbasepath, output.toByteArray());
        return new String[]{hbasepath};
    }

    @Override
    public String[] uploadimg(HttpServletRequest request, String filename) throws Exception {
        String hbasepath = null;
        if (filename.indexOf(".") > -1) {
            String suffix = filename.substring(filename.indexOf("."));
            // java 生成四位随机数
            // int i=(int)(Math.random()*9000)+1000;
            // int i= new java.util.Random().nextInt(9000)+1000;也可以
            hbasepath = DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM_SS_SSS_EN) + "_" + (new Random().nextInt(9000) + 1000) + "_file" + suffix;
            ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext());
            logger.info("HBase Application Running");
            HbaseUtils userUtils = context.getBean(HbaseUtils.class);
            userUtils.initialize("file");
            ByteArrayOutputStream output = new ByteArrayOutputStream();
            byte[] buffer = new byte[4096];
            int n = 0;
            while (-1 != (n = request.getInputStream().read(buffer))) {
                output.write(buffer, 0, n);
            }
            userUtils.addOrg(hbasepath, output.toByteArray());
            return new String[]{hbasepath, filename};
        } else {
            return new String[]{"error", filename};
        }
    }

    /**
     * 使用ueditor上传附件
     *
     * @param request
     * @param file    文件
     * @return
     * @throws Exception
     */
    @Override
    public String[] ueditorUpload(HttpServletRequest request, MultipartFile file) throws Exception {
        String filename = file.getOriginalFilename();
        String hbasepath = null;
        if (filename.indexOf(".") > -1) {
            String suffix = filename.substring(filename.indexOf("."));
            // java 生成四位随机数
            // int i=(int)(Math.random()*9000)+1000;
            // int i= new java.util.Random().nextInt(9000)+1000;也可以
            hbasepath = DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM_SS_SSS_EN) + "_" + (new Random().nextInt(9000) + 1000) + "_file" + suffix;
            ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext());
            logger.info("HBase Application Running");
            HbaseUtils userUtils = context.getBean(HbaseUtils.class);
            userUtils.initialize("file");
            userUtils.addOrg(hbasepath, file.getBytes());
            return new String[]{hbasepath, filename};
        } else {
            return new String[]{"error", filename};
        }
    }

}
